import { serializer } from "@memlab/core"
import { ArkTracePathItem } from "./ArkTracePath";

/**
 * ArkTracePath Filter function
 */
export type ArkTracePathFilter = (condition?: any) => boolean;
/**
 * ArkTracePath Output function
 */
export type ArkTracePathOutput = (path: ArkTracePathItem, filter?: ArkTracePathFilter, condition?: any) => string;

const defaultOutput: ArkTracePathOutput = (path: ArkTracePathItem) => {
    let snapshot = path?.node?.snapshot;
    return snapshot ? serializer.summarizePath(path, new Set<number>(), snapshot, {color: true}) : '';
};

/**
 * ArkTracer
 */
export class ArkTracer {
    public static outputTracePath(
        tracePath: ArkTracePathItem, 
        output?: ArkTracePathOutput, 
        filter?: ArkTracePathFilter,
        condition?: any) : void {
        let result: string = output ? output(tracePath, filter, condition) : defaultOutput(tracePath);
        if (result !== '') {
            console.log(result);
        } else {
            console.log('No leak trace path.');
        }
    }

    public static getTracePathString(
        tracePath: ArkTracePathItem, 
        output?: ArkTracePathOutput, 
        filter?: ArkTracePathFilter, 
        condition?: any) : string {
        return output ? output(tracePath, filter, condition) : defaultOutput(tracePath);
    } 
}